package com.springboot.cloud.demos.feign.rest;

import com.alibaba.fastjson.JSONObject;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.sensitive.SensitiveFilter;
import com.springboot.cloud.demos.entity.form.MomentForm;
import com.springboot.cloud.demos.entity.po.Moment;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.config.BusConfig;
import com.springboot.cloud.demos.feign.events.BusSender;
import com.springboot.cloud.demos.feign.service.MomentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class MomentController extends BaseController {

    @Autowired
    private MomentService momentService;
    @Autowired
    private BusSender busSender;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    @ApiOperation(value = "新增动态", notes = "新增一个动态")
    @ApiImplicitParam(name = "momentForm", value = "新增动态form表单", required = true, dataType = "MomentForm")
    @RequestMapping(value = "/moment", method = RequestMethod.POST)
    public Result add(HttpServletRequest request, @RequestBody MomentForm momentForm) {
        User user = getUser(request);
        momentForm.setUserId(user.getId());
        //过滤敏感词

// 待过滤的句子
        String sentence = momentForm.getTitle();
// 进行过滤
        String filted = sensitiveFilter.filter(sentence, '*');
        String subject = sensitiveFilter.filter(momentForm.getSubject(), '*');

// 如果未过滤，则返回输入的String引用
        if (sentence.equals(filted)) {
            // 句子中有敏感词
            log.info("filted:", filted);
        }
        momentForm.setTitle(filted);
        momentForm.setSubject(subject);
        Result<Moment> result = momentService.save(momentForm);
        if (result.isSuccess()) {
            //新建推送和消息
            Moment moment = result.getData();
            String message = JSONObject.toJSONString(moment);
            busSender.send(BusConfig.EXCHANGE_NAME, "push.message", message);
            busSender.send(BusConfig.EXCHANGE_NAME, "oss.message", message);


        }
        return result;
    }

    @ApiOperation(value = "查询一条动态", notes = "查询一个动态")
    @ApiImplicitParam(name = "id", value = "动态ID", required = true, dataType = "String")
    @RequestMapping(value = "/open/moment/{id}", method = RequestMethod.GET)
    public Result get(HttpServletRequest request, @PathVariable(value = "id") String id) {
        return momentService.get(id);
    }

    @ApiOperation(value = "查询一条动态", notes = "查询一个动态Map")
    @ApiImplicitParam(name = "id", value = "动态ID", required = true, dataType = "String")
    @RequestMapping(value = "/open/moment/getMomentMap", method = RequestMethod.GET)
    public Result getMomentMap(HttpServletRequest request, String id) {
        return momentService.getMomentMap(id);
    }

    @ApiOperation(value = "删除动态", notes = "删除一个动态")
    @ApiImplicitParam(name = "id", value = "动态ID", required = true, dataType = "String")
    @RequestMapping(value = "/moment/{id}", method = RequestMethod.DELETE)
    public Result delete(HttpServletRequest request, @PathVariable(value = "id") String id) {
        return momentService.delete(id);
    }

    @ApiOperation(value = "我的动态", notes = "查询我的动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "type", value = "动态类型，0：普通，1广播", required = true, dataType = "int"),
    })
    @RequestMapping(value = "/moment/my", method = RequestMethod.GET)
    public Result getLatestLevel(HttpServletRequest request, Integer type, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = getUser(request);
        return momentService.list(user.getId(), type, pageNo, pageSize);
    }

    @ApiOperation(value = "我的动态", notes = "查询我的动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "type", value = "动态类型，0：普通，1广播", required = true, dataType = "int")
    })
    @RequestMapping(value = "/open/moment/queryByUserId", method = RequestMethod.GET)
    public Result list(String userId, Integer type, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return momentService.list(userId, type, pageNo, pageSize);
    }

    @ApiOperation(value = "关注的动态", notes = "查询关注的动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sex", value = "1男，2女", required = true, dataType = "int"),
            @ApiImplicitParam(name = "real", value = "真人认证，0：不限，1认证", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pretty", value = "女神认证，0：不限，1认证", required = true, dataType = "int")

    })
    @RequestMapping(value = "/moment/follow", method = RequestMethod.GET)
    public Result followList(HttpServletRequest request, Integer sex, Integer real, Integer pretty, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = getUser(request);
        return momentService.followList(user.getId(), sex, real, pretty, pageNo, pageSize);
    }

    @ApiOperation(value = "推荐的动态", notes = "查询推荐的动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sex", value = "1男，2女", required = true, dataType = "int"),
            @ApiImplicitParam(name = "real", value = "真人认证，0：不限，1认证", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pretty", value = "女神认证，0：不限，1认证", required = true, dataType = "int")
    })
    @RequestMapping(value = "/moment/recommend", method = RequestMethod.GET)
    public Result recommendList(HttpServletRequest request, Integer sex, Integer real, Integer pretty, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = getUser(request);
        return momentService.recommendList(user.getId(), sex, real, pretty, pageNo, pageSize);
    }

    @ApiOperation(value = "广播动态", notes = "查询广播动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sex", value = "1男，2女", required = true, dataType = "int"),
            @ApiImplicitParam(name = "real", value = "真人认证，0：不限，1认证", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pretty", value = "女神认证，0：不限，1认证", required = true, dataType = "int")
    })
    @RequestMapping(value = "/open/moment/broadcast", method = RequestMethod.GET)
    public Result broadcastList(HttpServletRequest request, Integer sex, Integer real, Integer pretty, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return momentService.broadcastList(sex, real, pretty, null, pageNo, pageSize);
    }

    @ApiOperation(value = "附近的动态", notes = "查询附近的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sex", value = "1男，2女", required = true, dataType = "int"),
            @ApiImplicitParam(name = "real", value = "真人认证，0：不限，1认证", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pretty", value = "女神认证，0：不限，1认证", required = true, dataType = "int"),
            @ApiImplicitParam(name = "longitude", value = "经度", required = true, dataType = "float"),
            @ApiImplicitParam(name = "latitude", value = "纬度", required = true, dataType = "float")
    })
    @RequestMapping(value = "open/moment/nearby", method = RequestMethod.GET)
    public Result nearbyList(HttpServletRequest request, Integer sex, Integer real, Integer pretty, float longitude, float
            latitude, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return momentService.nearbyList(sex, real, pretty, longitude, latitude, pageNo, pageSize);
    }

    @ApiOperation(value = "获取下一个视频", notes = "查询附近的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "momentId", value = "动态ID", required = true, dataType = "string")
    })
    @RequestMapping(value = "open/moment/next", method = RequestMethod.GET)
    public Result next(String momentId) {
        return momentService.next(momentId);
    }

    @ApiOperation(value = "获取下一个视频", notes = "查询附近的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "momentId", value = "动态ID", required = true, dataType = "string")
    })
    @RequestMapping(value = "open/moment/prev", method = RequestMethod.GET)
    public Result prev(String momentId) {
        return momentService.prev(momentId);
    }


}
